package com.duobaoyu.dby.supply.biz.client.boundary.channel;

import com.duobaoyu.dby.channel.adapter.bean.area.AddressConfig;
import com.duobaoyu.dby.channel.adapter.bean.area.ChannelAddressInfo;
import com.duobaoyu.dby.channel.adapter.bean.area.ChannelCityInfo;
import com.duobaoyu.dby.channel.adapter.service.AbstractChannelAddressService;
import com.duobaoyu.dby.supply.biz.client.resource.ResourceApiClient;
import com.duobaoyu.dby.supply.biz.client.resource.bean.bo.AreaCityInfoBO;
import com.duobaoyu.dby.supply.biz.client.resource.bean.bo.AreaInfoBO;
import com.duobaoyu.dby.supply.biz.client.resource.bean.dto.AreaCityInfoDTO;
import com.duobaoyu.dby.supply.biz.client.resource.bean.dto.AreaInfoDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author liyang
 * @version 1.0.0
 * @ClassName SupplyChainChannelAddressConfig
 * @Description 渠道对接地址配置
 * @createTime 14:03:00 2023/09/26
 */
@Component
@RequiredArgsConstructor
public class SupplyChainChannelAddressConfig extends AbstractChannelAddressService {

	private final ResourceApiClient resourceApiClient;

	@Override
	public ChannelAddressInfo getChannelAddress(String provinceCode, String cityCode, String countyCode, String townCode, String channelCode) {
		AreaInfoDTO dto = AreaInfoDTO.builder()
				.provinceCode(provinceCode)
				.cityCode(cityCode)
				.countyCode(countyCode)
				.townCode(townCode)
				.channelCode(channelCode)
				.build();
		AreaInfoBO channelAddress = resourceApiClient.getChannelAddress(dto);
		if (Objects.isNull(channelAddress)) {
			return null;
		}
		return ChannelAddressInfo.builder()
				.channelCode(channelCode)
				.channelProvinceCode(channelAddress.getProvinceCode())
				.channelProvinceName(channelAddress.getProvinceName())
				.channelCityCode(channelAddress.getCityCode())
				.channelCityName(channelAddress.getCityName())
				.channelCountyCode(channelAddress.getCountyCode())
				.channelCountyName(channelAddress.getCountyName())
				.channelTownCode(channelAddress.getTownCode())
				.channelTownName(channelAddress.getTownName())
				.address(channelAddress.getAddress())
				.build();
	}

	@Override
	public ChannelCityInfo getChannelCityAddress(String provinceCode, String cityCode, String channelCode) {
		AreaCityInfoDTO dto = AreaCityInfoDTO.builder()
				.channelCode(channelCode)
				.provinceCode(provinceCode)
				.cityCode(cityCode)
				.build();
		AreaCityInfoBO channelCityAddress = resourceApiClient.getChannelCityAddress(dto);
		if (Objects.isNull(channelCityAddress)) {
			return null;
		}
		return ChannelCityInfo.builder()
				.channelProvinceCode(channelCityAddress.getProvinceCode())
				.channelProvinceName(channelCityAddress.getProvinceName())
				.channelCityCode(channelCityAddress.getCityCode())
				.channelCityName(channelCityAddress.getCityName())
				.channelCode(channelCode)
				.build();
	}

	@Override
	public AddressConfig getAddressConfig(String channelCode) {
		// TODO 这里需要读取地址配置
		return AddressConfig.builder()
				.similarityThreshold(30)
				.compareChannel(false)
				.build();
	}
}
